"""应用自动化基类"""
from abc import ABC, abstractmethod
from core import adb_utils, ocr_processor
from utils import common_utils, logger
from typing import Tuple, Optional
import time

class BaseApp(ABC):
    """应用自动化基类"""
    
    def __init__(self, ocr_processor: ocr_processor.OCRProcessor):
        """
        初始化应用自动化
        
        :param ocr_processor: OCR处理器实例
        :param target_text: 目标文本
        """
        self.ocr_processor = ocr_processor
        self.adb = adb_utils.ADBUtils()
        self.log = logger.get_logger(self.__class__.__name__)
    
    @abstractmethod
    def execute(self, device_id: str) -> bool:
        """执行完整操作链"""
        pass
    
    # -------------------- 共享方法 --------------------
    def _wake_device(self, device_id: str, wait_seconds: int = 2) -> None:
        """唤醒设备并等待稳定"""
        self.adb.wake_screen(device_id)
        time.sleep(wait_seconds)
    
    def _capture_screen(self, device_id: str) -> Optional[str]:
        """截取设备屏幕"""
        safe_id = adb_utils.ADBUtils.sanitize_device_id(device_id)
        path = f"{safe_id}_screenshot.png"
        return path if self.adb.capture_screen(path, device_id) else None
    
    def _process_ocr(self, image_path: str) -> Optional[Tuple[list, list, list]]:
        """执行OCR识别"""
        return self.ocr_processor.process_image(image_path)

    def _go_home(self, device_id: str) -> bool:
        """返回首页"""
        return self.adb.go_home(device_id)